package com.spring.sunflower.common

import android.Manifest
import android.content.Intent
import android.widget.GridView
import android.widget.TextView
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseActivity
import com.spring.sunflower.bean.LabelBean
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.dialog.OnPermissionIntroClickListener
import com.spring.sunflower.dialog.PermissionIntroDialog
import com.spring.sunflower.userinfo.HobbyPresenter
import java.util.*
import kotlin.collections.ArrayList

/**
 * Created by Kevin on 2021/8/8<br/>
 *
 * Describe:<br/>
 */
class TagSelectActivity : BaseActivity<HobbyPresenter>(), CommonContract.HobbyView {
    private var gvTag: GridView? = null
    private var mAdapter: TagSelectAdapter? = null
    private val mData: List<LabelBean.DatasBean> = ArrayList()
    private val mTempData: MutableList<LabelBean.DatasBean> = ArrayList()
    private var tvNextStep: TextView? = null
    private var genderType: String? = null
    private val hobbyData: MutableList<String> = java.util.ArrayList()

    override fun initView() {
        tvTitle.text = "选择标签"
        val intent = intent
        genderType = intent.getStringExtra(Constant.KEY_GENDER_TYPE)
        val femaleNickname = getField(Constant.KEY_REGISTER_NICKNAME_FEMALE, "")
        val nickname = getField(Constant.KEY_REGISTER_NICKNAME, "")
        gvTag = findViewById(R.id.gvTag)
        tvNextStep = findViewById(R.id.tvNextStep)
        mAdapter = TagSelectAdapter(this, mData)
        gvTag!!.adapter = mAdapter
        mAdapter?.setOnTagClickListener(object : TagSelectAdapter.OnTagClickListener {
            override fun onTagClick(position: Int) {
                val datasBean = mData[position]
                if (!mTempData.contains(datasBean)) {
                    if (mTempData.size >= 2) {
                        val indexOf = mData.indexOf(mTempData[0])
                        mData[indexOf].isChecked = false
                        mTempData.removeAt(0)
                        datasBean.isChecked = true
                        mTempData.add(datasBean)
                    } else {
                        mTempData.add(datasBean)
                        datasBean.isChecked = true
                    }
                    mAdapter!!.notifyDataSetChanged()
                }


            }

        })
        tvNextStep?.setOnClickListener {
            val string: String = handleHobbyData()
            if (Constant.GENDER_MALE == genderType) {
                mPresenter.register(genderType, string, nickname)
            } else {
                mPresenter.register(genderType, string, femaleNickname)
            }
        }
        mPresenter.getHobbyType()

    }

    private fun handleHobbyData(): String {
        val stringBuffer = StringBuffer()
        for (i in mTempData.indices) {
            if (i < mTempData.size - 1) {
                stringBuffer.append(mTempData[i].id.toString() + ",")
            } else {
                stringBuffer.append(mTempData[i].id)
            }
        }
        return stringBuffer.toString()
    }

    override fun getLayoutID(): Int = R.layout.activity_tag_select

    override fun setPresenter(): HobbyPresenter = HobbyPresenter(this)

    override fun showHobby(data: MutableList<LabelBean.DatasBean>?) {
        val random = Random()
        var mTemp: MutableList<LabelBean.DatasBean> = ArrayList()
        while (mTemp.size < 4) {
            val nextInt = random.nextInt(data!!.size)
            if (!mTemp.contains(data[nextInt])) {
                mTemp.add(data[nextInt])
            }
        }
        if (mTemp.size > 0) {
            val datasBean = mTemp[0]
            datasBean.isChecked = true
            mTempData.add(datasBean)
        }
        mAdapter?.addData(mTemp)

    }

    override fun registerSucceed() {
        val granted = XXPermissions.isGranted(this, Manifest.permission.ACCESS_FINE_LOCATION)
        if (!granted) {
            val dialog = PermissionIntroDialog(
                this,
                R.layout.dialog_permission_intro_location,
                object : OnPermissionIntroClickListener {
                    override fun onClickCancel() {
                        startActivity(
                            Intent(
                                this@TagSelectActivity,
                                LocationActivity::class.java
                            )
                        )
                        finish()
                    }

                    override fun onClickOK() {
                        XXPermissions.with(this@TagSelectActivity)
                            .permission(Permission.ACCESS_FINE_LOCATION)
                            .request(object : OnPermissionCallback {
                                override fun onGranted(permissions: List<String>, all: Boolean) {
                                    startActivity(
                                        Intent(
                                            this@TagSelectActivity,
                                            LocationActivity::class.java
                                        )
                                    )
                                    finish()
                                }

                                override fun onDenied(permissions: List<String>, never: Boolean) {
                                    startActivity(
                                        Intent(
                                            this@TagSelectActivity,
                                            LocationActivity::class.java
                                        )
                                    )
                                    finish()
                                }
                            })
                    }
                })
            showPermissionIntroDialog(dialog)
        } else {
            startActivity(
                Intent(
                    this@TagSelectActivity,
                    LocationActivity::class.java
                )
            )
            finish()
        }

    }

    override fun registerFailed(status: Int, msg: String?) {
    }
}